/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package co.umb.CLienteWS.DAO;

import co.umb.CLienteWS.Models.PacienteModel;
import co.umb.CLienteWS.Models.conexionDbPool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Alexander Toro
 */
public class PostgresCLienteWSPacienteDao implements AccionesCLenteWSDao {

    private Connection cnn = null;
    
    @Override
    public ArrayList<PacienteModel> consultarTodos() {
        ArrayList<PacienteModel> pacientes = new ArrayList<PacienteModel>();
        try {
            cnn = conexionDbPool.obtenerConexion();
            String query = "select id_persona,nombre,apellido_1,apellido_2,"
                           + "numero_id from t_persona";
            PreparedStatement ps = cnn.prepareStatement(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                PacienteModel pr = new PacienteModel();
                pr.setIdPaciente(rs.getInt(1));
                pr.setNombre(rs.getString(2));
                pr.setApellido1(rs.getString(3));
                pr.setApellido2(rs.getString(4));
                pr.setNumeroID(rs.getString(5));
                pacientes.add(pr);
            }
            cnn.close();
        } catch (SQLException ex) {
            Logger.getLogger(PostgresCLienteWSPacienteDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return pacientes;
    }

    @Override
    public Object conultar(String criterio) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

      public String consultarPorCedula(String cedula){
        PacienteModel p = null;
        try {
            cnn = conexionDbPool.obtenerConexion();
            ResultSet res;            
            String query="select * from t_persona where numero_id='"+cedula+"'";            
            PreparedStatement ps = cnn.prepareStatement(query);
            res = ps.executeQuery();
            if(res.next()){
                p = new PacienteModel();
                p.setIdPaciente(res.getInt(1));
                p.setNombre(res.getString(2));
                p.setApellido1(res.getString(3));
                p.setApellido2(res.getString(4));                
                p.setNumeroID(res.getString(5));
                String ppss = p.toString();
                return p.toString();
            }
            cnn.close();
        } catch (SQLException ex) {
            Logger.getLogger(PostgresCLienteWSPacienteDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null ;
    }
    
    @Override
    public boolean crear(Object item) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void eliminar() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void editar() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
